<?php

class controller_permission extends __registry_controller
{
    public $_ = array();
    public $_layout = NULL;
    public $limit = 20;
    
    public $_content_top = NULL;
    public $_content_bot = NULL;
    public $_content_right = NULL;
    public $_content_left = NULL;
    public $_content_header = 'OPEN';
    public $_content_footer = NULL;

    public function __construct()
    {	
		$this->_layout =NULL;
        parent::add_object(array(
            'model_permission',
            'handle_paging',
            'handle_permission'
        ));
	}
	
	public function index_action($cid = null)
	{
	   /* Check permission */
	   $this->per_check_permission();
       
       $seo2 = isset($this->_seokey[2])?$this->_seokey[2]:null;
       $this->_['com'] = $this->_seokey[1];
       
       /*****************pagination**********************/
       $limit = $this->limit;
       $current = end($this->_seokey)?intval(end($this->_seokey)):1;
       $total = $this->mdp_get_total('permission');
       $url = !empty($cid)?URL_BASE.$this->_seokey[0].'/'.$this->_seokey[1].'/'.$seo2:URL_BASE.$this->_seokey[0].'/'.$this->_seokey[1];
       $this->_['paging'] = $this->paging_display($total, $current, $limit, $url, $this->_,'/','.htm');
       
       $this->_['url_add'] = URL_BASE.'admin/'.$this->_seokey[1].'/'.$this->route->rt_get_seokey_act('add');
       $this->_['url_edit'] = URL_BASE.'admin/'.$this->_seokey[1].'/'.$this->route->rt_get_seokey_act('edit');
	   
       $strHtmlContent = '';
       $strOkHtml = '';
       $html = $this->view->view_display("index");
       $arrHtml = $this->view->view_get_block_html($html);

       $list_permission = $this->mdp_get_list_permission($current,$limit);
       
       $list_action = $this->mdp_get_seokey_action();
       $action_array = array();
       if(!empty($list_action))
       {
            foreach($list_action as $action)
            {
                $action_array[$action['ActName']] = $action['Name'];
            }
       }
       if(!empty($list_permission)){
            foreach($list_permission as $permission){
                $permission['Action'] = $action_array[$permission['Action']];
                $strOkHtml .= $this->view->view_assign($arrHtml['category'],$permission);
            
            }
       }
       /********create filter******************/
        $list_seokey = $this->mdp_get_seokey_from_app('all');
        $strOkHtmlFilter = '';
        if(!empty($list_seokey))
        {
            foreach($list_seokey as $key=>$value)
            {
                
                if($key == 0)
                {
                    $group_seokey = $value['Define'];
                    $strOkHtmlFilter = $this->view->view_assign($arrHtml['optgroup_open'],
                                                        array(
                                                                'group_seokey'=>$this->_['group_'.$value['Define']]
                                                        )
                    );
                }
                if($group_seokey != $value['Define']){
                    $group_seokey = $value['Define'];
                    $strOkHtmlFilter .= $arrHtml['optgroup_close'];
                    $strOkHtmlFilter .= $this->view->view_assign($arrHtml['optgroup_open'],
                                                        array(
                                                                'group_seokey'=>$this->_['group_'.$value['Define']]
                                                        )
                    );
                    $group_seokey = $value['Define'];
                }
                
                $selected = '';
                if($cid != null)
                {  
                    if($cid == $value['CatID'])
                    {
                        $selected = 'selected=""';
                    }
                }
                
                $strOkHtmlFilter .= $this->view->view_assign($arrHtml['seo_key'],
                                                        array(
                                                                'seo_key_name'           => $value['SeoKey'],
                                                                'seo_key_id'           => $value['CatID'],
                                                                'selected'                => $selected
                                                        )
                );
                
                if((count($list_seokey)-1) == $key)
                {
                    $strOkHtmlFilter .= $arrHtml['optgroup_close'];
                }
            }
        }
       
       
       
       $strHtmlContent = $this->view->view_assign_block($html, 
                                                        array(
                                                            'htmlCat'=>$strOkHtml,
                                                            'list_seo_key'=>$strOkHtmlFilter
                                                        )
       );
       return $strHtmlContent;
    }

    public function add_action()
    {
        /* Check permission */
        if( !$this->per_check_permission('add',true) )
        {
           $this->_layout ='error';
           $strHtmlContent = $this->_['not_permission'];
        }else{
            $this->_['label-error'] = '';
            $this->_layout = 'iframe';
            $this->_['com'] = 'permission';
            $this->_['error'] = '';
            if(isset($_POST) && !empty($_POST)){
                $permission_name = isset($_POST['permission_name']) && $_POST['permission_name'] != ''?$_POST['permission_name']:'';
                $seo_key = isset($_POST['seo_key']) && $_POST['seo_key'] != ''?$_POST['seo_key']:'';
                $action = isset($_POST['action']) && $_POST['action'] != ''?$_POST['action']:'';
                $status = $_POST['Status'];
                if($permission_name != '' && $seo_key != '' && $action != '')
                {
                    $data= array(
                        'PremissName'=>$permission_name,
                        'CID'=>$seo_key,
                        'Action'=>$action,
                        'Status'=>$status
                    );
                    $result = $this->mdp_save_permission('permission', $data);
                    $this->request->rq_goto_window_parent(URL_BASE.$this->_seokey[0].'/'.$this->_seokey[1].'/');     
                }else
                {
                    $this->_['error'] = $this->_['error_label'];   
                }
                
            }
            /******************************list seo key**********************/
            $list_seo_key = $this->mdp_get_list_seo_key();
            $strHtml = $this->view->view_display('add');
            $arrHtml = $this->view->view_get_block_html($strHtml);
            $strOkHtml = '';
            if(!empty($list_seo_key))
            {
                foreach($list_seo_key as $key=>$value)
                {
                    $strOkHtml .= $this->view->view_assign($arrHtml['seo_key'],
                                                            array(
                                                                    'seo_key_name'           => $value['SeoKey'],
                                                                    'seo_key_id'           => $value['ID']
                                                            )
                    );
                }
            }
            
            $strHtmlContent = $this->view->view_assign_block($strHtml, array('list_seo_key'=>$strOkHtml));
        }
        
        
        return $strHtmlContent;   
    }
    
    public function edit_action(){
        /* Check permission */
        if( !$this->per_check_permission('edit',true) )
        {
           $this->_layout ='error';
           $strHtmlContent = $this->_['not_permission'];
        }else{
            
            $this->_['label-error'] = '';
            $this->_layout = 'iframe';
            $this->_['com'] = 'permission';
            $this->_['error'] = '';
            $id = $this->_seokey[3];
            if(isset($_POST) && !empty($_POST)){
                $permission_name = isset($_POST['permission_name']) && $_POST['permission_name'] != ''?$_POST['permission_name']:'';
                $seo_key = isset($_POST['seo_key']) && $_POST['seo_key'] != ''?$_POST['seo_key']:'';
                $action = isset($_POST['action']) && $_POST['action'] != ''?$_POST['action']:'';
                $status = $_POST['Status'];
                if($permission_name != '' && $seo_key != '' && $action != '')
                {
                    $data= array(
                        'PremissName'=>$permission_name,
                        'CID'=>$seo_key,
                        'Action'=>$action,
                        'Status'=>$status
                    );
                    $condition = 'ID = '.$id;
                    $result = $this->mdp_update_permission('permission', $data,$condition);
                    $this->request->rq_goto_window_parent(URL_BASE.$this->_seokey[0].'/'.$this->_seokey[1].'/');    
                }else
                {
                    $this->_['error'] = $this->_['error_label'];    
                }
                
            }
            /******************************list seo key***********************************************/
            $get_permission_item = $this->mdp_get_permission_from_id($id);
            if(empty($get_permission_item)){
                exit;
            }
            $seo_key_name = $this->mdp_get_seokey_catid($get_permission_item['CID']);
            $seo_key_name = $seo_key_name['SeoKey'];
            $list_action_from_com = $this->mdp_get_action_seokey($seo_key_name);
            $list_seo_key = $this->mdp_get_list_seo_key();  
            
            $this->_['permission_value']  = $get_permission_item['PremissName'];
            
            
            
            $strHtml = $this->view->view_display('edit');
            $arrHtml = $this->view->view_get_block_html($strHtml);
            $strOkHtmlSeoKey = '';
            $strOkHtmlAction = '';
            if(!empty($list_seo_key))
            {
                foreach($list_seo_key as $key=>$seo_key)
                {
                    if($seo_key['SeoKey'] == $seo_key_name){
                        $selected_seokey = 'selected=""';
                    }else $selected_seokey = '';
                    $strOkHtmlSeoKey .= $this->view->view_assign($arrHtml['seo_key'],
                                                                  array(
                                                                            'seo_key_name'           => $seo_key['SeoKey'],
                                                                            'seo_key_id'           => $seo_key['ID'],
                                                                            'select_seokey'               => $selected_seokey
                                                                  )
                    );
                }
            }
            if(!empty($list_action_from_com))
            {
                foreach($list_action_from_com as $key=>$action)
                {
                    if($action['ActName'] == $get_permission_item['Action']){
                        $selected_action = 'selected=""';
                    }else $selected_action = '';
                    $strOkHtmlAction .= $this->view->view_assign($arrHtml['action'],
                                                                 array(
                                                                        'action_value'           => $action['ActName'],
                                                                        'action_id'              => $action['ID'],
                                                                        'select_action'          => $selected_action
                                                                 )
                    );
                }
            }  
            $arr_status = array('true'=>'true','false'=>'false');
            $strOkHtmlStatus = '';
            foreach($arr_status as $key=>$status)
            {
                $select_status = '';
                if($get_permission_item['Status'] == $status)
                {
                    $select_status = 'selected=""';
                }
                $strOkHtmlStatus .= '<option '.$select_status.' value="'.$key.'">'.$this->_[$status].'</option>';
            }
            $this->_['html_status'] = $strOkHtmlStatus;
    
            $strHtmlContent = $this->view->view_assign_block($strHtml, 
                                                             array(
                                                                'list_seo_key'=>$strOkHtmlSeoKey,
                                                                'list_action'=>$strOkHtmlAction
                                                                )
            );
        }
        
        
        return $strHtmlContent;
    }
    
    public function index_ajax_action(){
        $this->_layout = 'NO_LAYOUT';
        /* Check permission */
        $this->per_check_permission();
        
        $action = isset($this->_seokey[3])?$this->_seokey[3]:null;
        $str_id = isset($this->_seokey[4])?$this->_seokey[4]:null;
        $str_path = isset($this->_seokey[5])?$this->_seokey[5]:null;
        $html = '';
        if(isset($action)){
            switch($action)
            {
               case "getseokey":
                   $html = $this->getSeokey();
                   break;
               case "getaction":
                   $html = $this->getAction();
                   break;
               case "updateStatus":
                   $html = $this->updateStatus();
                   break;
               case "filter":
                   $html = $this->filter($str_id);
                   break;
               case "hide":
                   $done = $this->mdp_tool_func($this->model,$action,$str_id);
                    if($done){
                        return 'ok';
                    }else{
                        return 'noOk';
                    }
                   break;
               case "show":
                   $done = $this->mdp_tool_func($this->model,$action,$str_id);
                    if($done){
                        return 'ok';
                    }else{
                        return 'noOk';
                    }
                   break;
               case "delete":
                   $done = $this->mdp_tool_func($this->model,$action,$str_id);
                    if($done){
                        return 'ok';
                    }else{
                        return 'noOk';
                    }
                   break;
               default:
            }
            return $html;    
        }
    }
    
    public function getAction()
    {
        if(isset($_POST['id_seokey']) && !empty($_POST['id_seokey'])){
            $id_seokey = (int)$_POST['id_seokey'];
            $get_action_from_seokey = $this->mdp_get_action_from_seokey($id_seokey);
            $list_action = $this->mdp_get_seokey_action();
            $action_array = array();
            if(!empty($list_action))
            {
                foreach($list_action as $action)
                {
                    $action_array[$action['ActName']] = $action['SeoKey'];
                }
            }
            if(!empty($get_action_from_seokey))
            {
                $html = '<select name="action" class="field select small" tabindex="3">';
                foreach($get_action_from_seokey as $value)
                {
                    $html .= '<option value="'.$value['ActName'].'">'.$action_array[$value['ActName']].'</option>';
                }
                $html .= '</select>';
            }else
            {
                $html = 'no-data';
            }
        }else
        {
            $html =  'no-data';
        }
        
        return $html;
    }
    
    public function getSeokey()
    {
        if(isset($_POST['app'])){
            $app = $_POST['app'];
            $list_seokey_from_app = $this->mdp_get_seokey_from_app($app);
            if(!empty($list_seokey_from_app))
            {
                $html = '<select id="seo_key" name="seo_key" class="field select small" tabindex="3">';
                foreach($list_seokey_from_app as $value)
                {
                    $html .= '<option  title="'.$value['CatID'].'" value="'.$value['CatID'].'">'.$value['SeoKey'].'</option>';
                }
                $html .= '</select>';
            }else
            {
                $html = 'no-data';
            }
        }else
        {
            $html =  'no-data';
        }
        
        return $html;
    }
    
    public function filter($cid)
    {
       $seo2 = isset($this->_seokey[2])?$this->_seokey[2]:null;
       $this->_['com'] = $this->_seokey[1];
       $this->_['url_edit'] = URL_BASE.'admin/'.$this->_seokey[1].'/'.$this->route->rt_get_seokey_act('edit');
       
       /*****************pagination**********************/
       $limit = $this->limit;
       $current = 1;
       $total = $this->mdp_get_total('permission', $cid);
       $url = !empty($cid)?URL_BASE.$this->_seokey[0].'/'.$this->_seokey[1]:URL_BASE.$this->_seokey[0].'/'.$this->_seokey[1];
       $this->_['paging'] = $this->paging_display($total, $current, $limit, $url, $this->_,'/','.htm');
	   
       $strHtmlContent = '';
       $strOkHtml = '';
       $html = $this->view->view_display("ajax_filter");
       $arrHtml = $this->view->view_get_block_html($html);

       $list_permission = $this->mdp_get_list_permission($current,$limit,$cid);
       
       $list_action = $this->mdp_get_seokey_action();
       $action_array = array();
       if(!empty($list_action))
       {
            foreach($list_action as $action)
            {
                $action_array[$action['ActName']] = $action['SeoKey'];
            }
       }
       if(!empty($list_permission)){
            foreach($list_permission as $permission){
                $permission['Action'] = $action_array[$permission['Action']];
                $strOkHtml .= $this->view->view_assign($arrHtml['category'],$permission);
            
            }
       }
       
       
       
       $strHtmlContent = $this->view->view_assign_block($html, 
                                                        array(
                                                            'htmlCat'=>$strOkHtml
                                                        )
       );
	   return $strHtmlContent;
    }
    
    
}